using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using dender.dvdcover.core.image;

namespace dender.dvdcover.core
{
    /// <summary>
    /// Defines an image that can be loaded into a part
    /// </summary>
    public class CoverImage
    {
        private Bitmap mOriginalImage;
        private Bitmap mTransformedImage;
        private bool mIsTransformed;

        public event EventHandler<ImageChangedEventArgs> OnImageChanged;

        /// <summary>
        /// Gets underlying bitmap image
        /// </summary>
        public Bitmap Image
        {
            get
            {
                if (mIsTransformed)
                    return mTransformedImage;
                else
                    return mOriginalImage;
            }
        }

        private Part mPart;
        /// <summary>
        /// Gets the part of the image
        /// </summary>
        public Part Part
        {
            get
            {
                return mPart;
            }

        }

        /// <summary>
        /// Creates a CoverImage by specifying a part.
        /// </summary>
        /// <param name="part">Part where the image is located</param>
        public CoverImage(Part part)
        {
            this.mPart = part;
            this.mIsTransformed = false;
        }

        /// <summary>
        /// Creates a CoverImage by specifying a part and a bitmap
        /// </summary>
        /// <param name="part">Part where the image is located</param>
        /// <param name="image">Bitmap of the image</param>
        public CoverImage(Part part, Bitmap image) : this(part)
        {
            this.mOriginalImage = image;
        }

        /// <summary>
        /// Creates a CoverImage by specifying the part and a path
        /// </summary>
        /// <param name="part">Part of the image</param>
        /// <param name="filename">A string containing the name of the file 
        /// from wich to create an CoverImage</param>
        /// <exception cref="System.OutOfMemoryException">
        /// </exception>
        /// <exception cref="System.IO.FileNotFoundException">
        /// </exception>
        public CoverImage(Part part, string filename) : this(part)
        {
            mOriginalImage = (Bitmap)Bitmap.FromFile(filename);
        }

        public void Resize(IResizeStyle style)
        {
            style.Resize(this.Image, Part.AspectRatio, out this.mTransformedImage);
            this.mIsTransformed = true;
            EventTrigger<ImageChangedEventArgs>.Invoke(OnImageChanged, this, new ImageChangedEventArgs(this.Image));
        }

        public void Rotate(IRotateStyle style)
        {
            style.Rotate(this.Image, out this.mTransformedImage);
            this.mIsTransformed = true;
            EventTrigger<ImageChangedEventArgs>.Invoke(OnImageChanged, this, new ImageChangedEventArgs(this.Image));
        }

        public void ResetTransform()
        {
            this.mIsTransformed = false;
            EventTrigger<ImageChangedEventArgs>.Invoke(OnImageChanged, this, new ImageChangedEventArgs(this.Image));
        }
    }
}
